/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
user = null;
 curso = null;
 wwwroot = null;
 userid= null;
$(document).ready(function() {
   //$.mobile.page.prototype.options.addBackBtn=true;
   $.mobile.page.prototype.options.domCache = true;
  
 $('input#login-btn').live('click', function(e) {
     
          $.mobile.showPageLoadingMsg();
          if( validar_datos( "form-login" ) ){
                              
                 $.ajax({
                        contentType: "application/x-www-form-urlencoded",
                        type: "POST",       
                        url: "lib/lib.php",
                        data: $( "#login-form" ).serialize(),
                        dataType: "json",
                        async:false,           
                        success: function( resp ){
                            
                               user =  eval ( resp );
                               
                            wwwroot = user.wwwroot;
                              if( user.error == "none"){
                                  userid = user.userid;
                                 //show_muro( user[user.userid] )
                                 window.location.replace("index.php");
                              }else{

                                   //window.location.replace("#error");
                                   $.mobile.changePage( "#error",  "slide", 
                                       {reloadPage: true,
                                        role:"dialog"  
                                       
                                       
                                   });
                              }                        
                        },//end success
                        error:function(xhr, ajaxOptions, thrownError){
                            alert(xhr.status + " "+ thrownError);
                             $.mobile.hidePageLoadingMsg();
                        }
                  });
             
            }else{
                $.mobile.hidePageLoadingMsg();
              alert("todos los campos son necesarios");
            }       
             $.mobile.hidePageLoadingMsg();
    });//end click
 
 
   $('a.courseview').live('click', function(e) {
       
       $.mobile.showPageLoadingMsg();
          var courseid = $(this).attr("id").split("_");
            curso = courseid[1];
            
            $(".courseop").remove();
          $("#menucourse").remove();
          $.ajax({
                        contentType: "application/x-www-form-urlencoded",
                        type: "POST",       
                        url: "course/course.php",
                        data: "courseid="+courseid[1]+"&oper=get_curso",                     
                        async:false,           
                        success: function( resp ){
                                 //alert(resp);
                                 //$.mobile.showPageLoadingMsg();
                                 $( "#content" ).append( resp );
                                 $.mobile.hidePageLoadingMsg();
                                 window.location.replace("#menucourse");
                                  
                        },//end success
                        error:function(xhr, ajaxOptions, thrownError){
                            alert(xhr.status + " "+ thrownError);
                             $.mobile.hidePageLoadingMsg();
                        }
                  });
      
   });//end click courseview
   
   $('a.link_interno').live('click', function(e) {
       $.mobile.showPageLoadingMsg();
          var pag = $(this).attr("id").split("_");
          var oper = "";
          if( pag[1] == "anuncios" ){
              oper = "get_anuncios";
          }
          if( pag[1] == "tareas" ){
              oper = "get_tareas";
          }
          if( pag[1] == "foros" ){
              oper = "get_foros";
          }
          $.ajax({
                        contentType: "application/x-www-form-urlencoded",
                        type: "POST",       
                        url: "course/course.php",
                        data: "courseid="+curso+"&oper="+oper,                     
                        async:false,           
                        success: function( resp ){
                                 //alert(resp);
                                  
                                 $( "#content" ).append( resp );
                                 $.mobile.hidePageLoadingMsg();
                                 window.location.replace("#"+pag[1]);
                                  
                        },//end success
                        error:function(xhr, ajaxOptions, thrownError){
                            alert(xhr.status + " "+ thrownError);
                             $.mobile.hidePageLoadingMsg();
                        }
          });
   });//end link interno
   
   $('#btn_add_anuncio').live('click', function(e) {
       if( $("#txt_anuncio").val() != "" && $("#curso").val() != "" ){
             $.ajax({
                        contentType: "application/x-www-form-urlencoded",
                        type: "POST",       
                        url: "lib/lib.php",
                        data: $("#form_sanuncio").serialize(),                     
                        async:false,           
                        success: function( resp ){
                                 //alert(resp);
                                  if(resp != "No se pudo ingresar el anuncio"){
                                     $( "#content_anun" ).prepend( resp );
                                     var $page = $( "#anuncios" );
                                     $content = $page.children( ":jqmData(role=content)" );
                                     $page.page();
                                     $content.find( ":jqmData(role=listview)" ).listview();
                                     
                                  }else{
                                       alert( resp );
                                  }
                                  $.mobile.hidePageLoadingMsg();
                                   window.location.replace("#anuncios");
                                  
                        },//end success
                        error:function(xhr, ajaxOptions, thrownError){
                            alert(xhr.status + " "+ thrownError);
                             $.mobile.hidePageLoadingMsg();
                        }
              });
       }//end if
   });
   
   
    /*
     *Agregar comentarios rsa
     *
    */
    $('.coments_btn_rsa').live('click', function(e) {
        
        var postid = $(this).attr("id").split("_");
        
        if( $("#coment_txt_rsa_" + postid[1]).val() != ''){
           
            //$( "ul#coments_rsa_" + postid[1] ).prepend( $("#coment_txt_rsa_" + postid[1]).val() );
            $.ajax({
                        contentType: "application/x-www-form-urlencoded",
                        type: "POST",       
                        url: "lib/lib.php",
                        data: "post_id="+postid[1]+"&curso="+curso+"&coment_rsa="+$("#coment_txt_rsa_" + postid[1]).val()+"&user="+postid[2]+"&oper=save-coment-rsa",             
                        async:false,           
                        success: function( resp ){
                                
                                
                                  if(resp.search("error") == -1){
                                    
                                      $( "ul#coments_rsa_" + postid[1] ).prepend( resp );                              
                                     var $page = $( "#anuncios" );
                                     $content = $page.children( ":jqmData(role=content)" );
                                     $page.page();
                                     $content.find( ":jqmData(role=listview)" ).listview("refresh");
                                      $("#coment_txt_rsa_" + postid[1]).val("");
                                  }else{
                                       alert( resp );
                                  }
                                  $.mobile.hidePageLoadingMsg();
                                  // window.location.replace("#anuncios");

                        },//end success
                        error:function(xhr, ajaxOptions, thrownError){
                            alert(xhr.status + " "+ thrownError);
                             $.mobile.hidePageLoadingMsg();
                        }
              });
        }
    });//end 
   
   
   $(document).bind( "pagebeforechange", function( e, data ) {
        //alert(data.toPage);
        if ( typeof data.toPage === "string" ) {
            
            if( data.toPage.search("add_anuncio_dialog") !== -1 ){
                add_dialog( curso )
            }
        }	
    }); //end pagebeforechange*/
});//end document.ready


function validar_datos( section ){
    var ban = false;
    if( section == "form-login" ){
        if( $("#username").val() != "" && $("#password").val() != ""  ){
            ban = true;
        }
    }//end section
    
    return ban;
}//ed validar_datos

function add_dialog( curso ){
    $.mobile.showPageLoadingMsg();
     $.ajax({
            contentType: "application/x-www-form-urlencoded",
            type: "POST",       
            url: "course/course.php",
            data: "courseid="+curso+"&oper=anuncio_dialog",                     
            async:false,           
            success: function( resp ){
                     //alert(resp);

                     $( "#content" ).append( resp );
                     $.mobile.hidePageLoadingMsg();
                     //window.location.replace("#"+pag[1]);

            },//end success
            error:function(xhr, ajaxOptions, thrownError){
                alert(xhr.status + " "+ thrownError);
                 $.mobile.hidePageLoadingMsg();
            }
    });
}